package tree.nodeType.numeric.functions;

import java.util.ArrayList;
import java.util.Random;

import pools.PoolsManager;
import tree.Node;
import tree.TreeNodesManager;
import tree.nodeType.Numeric;
import tree.nodeType.numeric.NumericFunction;

public class Sinus extends NumericFunction{
	public Sinus(Node parent,int depth,int maxDepth, TreeNodesManager treeNodesManager) {
		super(1,parent,treeNodesManager,depth);
		initSons(depth, maxDepth,treeNodesManager);
		
	}
	public Sinus() {}
	
	public double execute() {
		return Math.sin(((Numeric)sons.get(0)).execute());		
	}




	@Override
	public void initSons(int depth, int maximumDepth, TreeNodesManager treeNodesManager) {
		
			if (depth+1==maximumDepth){
				sons.add(PoolsManager.get().getRandomConstantNode(this, treeNodesManager, depth+1));
			}else if (random.nextDouble()<0.5 || depth<2){
				sons.add(PoolsManager.get().getRandomNumericFuncNode(depth+1, maximumDepth, this,treeNodesManager));
			}else{
				sons.add(PoolsManager.get().getRandomConstantNode(this, treeNodesManager, depth+1));
			}
		

	}
	
	@Override
	public String toString() {	
		return "sinus operator";
	}
	
	@Override
	protected Node clone(TreeNodesManager treeNodesManager,Node parent) {
		Node cloned= new Sinus(this,treeNodesManager,parent);		
		return cloned;
	}
	
	@Override
	protected Node clone(Node parent) {
		Node cloned= new Sinus(this,parent);		
		return cloned;
	}
	
	public Sinus(Sinus sinus,Node parent){
		this.depth=sinus.depth;
		this.days=sinus.days;
		this.parent=parent;
		this.sonsNeeded=sinus.sonsNeeded;
		this.random= new Random();
		this.sons= new ArrayList<Node>();
		for (Node son : sinus.sons){
			this.sons.add(son.cloneSubTree(this));
		}
	}
	public Sinus(Sinus sinus, TreeNodesManager treeNodesManager,Node parent){
		super(1,parent,treeNodesManager,sinus.depth,sinus.days);		
		for (Node son : sinus.sons){
			this.sons.add(son.cloneSubTree(treeNodesManager,this));
		}
	}

}
